package net.hands.common.mail;

import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.NoSuchProviderException;
import javax.mail.Session;
import javax.mail.Transport;
/**
 * 说明:smtp服务
 * <br>
 * <p>这是一个开源的软件，请在LGPLv3下合法使用、修改或重新发布。</p>
 * @author 陈侃(cknet@126.com, 13910090885)
 *         http://code.google.com/p/simpleframework/
 *         http://www.simpleframework.net
 */
public class SmtpServer implements SendMailSessionProvider {
   /**
    * 主机
    */
	protected static final String MAIL_HOST = "mail.host";
	/**
	 * stmp主机
	 */
	protected static final String MAIL_SMTP_HOST = "mail.smtp.host";
	/**
	 * stmp端口
	 */
	protected static final String MAIL_SMTP_PORT = "mail.smtp.port";
	/**
	 * stmp认证
	 */
	protected static final String MAIL_SMTP_AUTH = "mail.smtp.auth";
	/**
	 * 传输协议
	 */
	protected static final String MAIL_TRANSPORT_PROTOCOL = "mail.transport.protocol";
	/**
	 * smtp协议
	 */
	protected static final String PROTOCOL_SMTP = "smtp";
   /**
    * 默认端口
    */
	protected static final int DEFAULT_SMTP_PORT = 25;
   /**
    * 主机
    */
	protected final String host;
	/**
	 * 端口
	 */
	protected final int port;
	/**
	 * 认证
	 */
	protected final Authenticator authenticator;
	/**
	 * 会话参数
	 */
	protected final Properties sessionProperties;
   /**
    * 
   * Constructor Method  
   * @update 2012-9-29 下午4:54:57  
   * @param host 主机
    */
	public SmtpServer(final String host) {
		this(host, DEFAULT_SMTP_PORT, null);
	} 
   /**
    * 
   * Constructor Method  
   * @update 2012-9-29 下午4:55:11  
   * @param host 主机
   * @param port 端口
    */
	public SmtpServer(final String host, final int port) {
		this(host, port, null);
	}
   /**
    * 
   * Constructor Method  
   * @update 2012-9-29 下午4:55:38  
   * @param host 主机
   * @param authenticator 认证
    */
	public SmtpServer(final String host, final Authenticator authenticator) {
		this(host, DEFAULT_SMTP_PORT, authenticator);
	}
   /**
    * 
   * Constructor Method  
   * @update 2012-9-29 下午4:55:50  
   * @param host 主机
   * @param port 端口
   * @param username 用户名
   * @param password 密码
    */
	public SmtpServer(final String host, final int port, final String username, final String password) {
		this(host, port, new SimpleAuthenticator(username, password));
	}
   /**
    * 
   * Constructor Method  
   * @update 2012-9-29 下午4:56:32  
   * @param host 主键
   * @param username 用户名
   * @param password 密码
    */
	public SmtpServer(final String host, final String username, final String password) {
		this(host, DEFAULT_SMTP_PORT, new SimpleAuthenticator(username, password));
	}
   /**
    * 
   * Constructor Method  
   * @update 2012-9-29 下午4:56:54  
   * @param host 主机
   * @param port 端口
   * @param authenticator 认证
    */
	public SmtpServer(final String host, final int port, final Authenticator authenticator) {
		this.host = host;
		this.port = port;
		this.authenticator = authenticator;
		sessionProperties = createSessionProperties();
	}
   /**
    * 
   * 说明：属性 
   * @return Properties
   * @update 2012-9-29 下午4:57:20
    */
	protected Properties createSessionProperties() {
		final Properties props = new Properties();
		props.setProperty(MAIL_TRANSPORT_PROTOCOL, PROTOCOL_SMTP);
		props.setProperty(MAIL_HOST, host);
		props.setProperty(MAIL_SMTP_HOST, host);
		props.setProperty(MAIL_SMTP_PORT, String.valueOf(port));
		if (authenticator != null) {
			props.setProperty(MAIL_SMTP_AUTH, "true");
		}
		return props;
	}
   /**
    * 
   (non-Javadoc)  
   * @see net.hands.common.mail.SendMailSessionProvider#createSession()  
   * @update 2012-9-29 下午5:08:12
    */
	@Override
	public SendMailSession createSession() {
		final Session mailSession = Session.getInstance(sessionProperties, authenticator);
		Transport mailTransport;
		try {
			mailTransport = getTransport(mailSession);
		} catch (final NoSuchProviderException nspex) {
			throw new MailException(nspex);
		}
		return new SendMailSession(mailSession, mailTransport);
	}
   /**
    * 
   * 说明：获得运输
   * @param session 会话
   * @return Transport
   * @throws NoSuchProviderException
   * @update 2012-9-29 下午5:08:21
    */
	protected Transport getTransport(final Session session) throws NoSuchProviderException {
		return session.getTransport(PROTOCOL_SMTP);
	}

	// ---------------------------------------------------------------- getters
   /**
    * 
   * 说明：获得主机
   * @return String
   * @update 2012-9-29 下午5:11:17
    */
	public String getHost() {
		return host;
	}
   /**
    * 
   * 说明：获得认证
   * @return Authenticator
   * @update 2012-9-29 下午5:11:25
    */
	public Authenticator getAuthenticator() {
		return authenticator;
	}
   /**
    * 
   * 说明：获得端口
   * @return int
   * @update 2012-9-29 下午5:11:42
    */
	public int getPort() {
		return port;
	}
}